﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Sys;

namespace net_work.Config.DataBaseConfig.Sys;

public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable("sys_user");

        builder.HasKey(u => u.Id);
        builder.HasIndex(u => u.UserAccount).IsUnique();
        builder.HasIndex(u => u.UserPhone).IsUnique();
        builder.HasIndex(u => u.UserEmail).IsUnique();

        // 配置字段名映射
        builder.Property(u => u.Id).HasColumnName("user_id");
        builder.Property(u => u.UserAccount).HasColumnName("user_account").IsRequired().HasMaxLength(64);
        builder.Property(u => u.UserPassword).HasColumnName("user_password").IsRequired().HasMaxLength(256);
        builder.Property(u => u.UserEmail).HasColumnName("user_email").HasMaxLength(128);
        builder.Property(u => u.UserPhone).HasColumnName("user_phone").IsRequired().HasMaxLength(32);
        builder.Property(u => u.UserRole).HasColumnName("user_role").IsRequired().HasMaxLength(32);
        builder.Property(u => u.PsnId).HasColumnName("psn_id").IsRequired();
        
        builder.Property(u => u.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(u => u.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(u => u.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(u => u.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(u => u.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(u => u.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}